﻿using SimpleTerminal.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleTerminal
{

    public partial class KeywordListForm : Form
    {
        List<Keyword> keywords;
        public KeywordListForm()
        {
            InitializeComponent();
            RefreshData();

        }



        public void RefreshData()
        {
            using (Datastore datastore = new Datastore())
            {
                keywords = datastore.Keywords(true);
                listView1.Items.Clear();
                listView1.BeginUpdate();
                foreach (Keyword keyword in keywords)
                {
                    ListViewItem lvi = new ListViewItem
                    {
                        Text = "",
                        Tag = keyword,
                        Checked = keyword.Enable
                    };

                    lvi.SubItems.Add(keyword.Pattern);
                    lvi.SubItems.Add(keyword.Explain);
                    this.listView1.Items.Add(lvi);
                }
                listView1.EndUpdate();
            }

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            KeywordForm keywordForm = new KeywordForm();
            keywordForm.StartPosition = FormStartPosition.CenterScreen;
            if (keywordForm.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
                UpdateBtnUi();
            }
        }

        private void UpdateBtnUi()
        {
            if (listView1.SelectedItems.Count > 0)
            {
                button2.Enabled = true;
                button3.Enabled = true;
                button4.Enabled = listView1.SelectedItems[0].Index != 0;
                button5.Enabled = listView1.SelectedItems[0].Index != listView1.Items.Count - 1;
            }
            else
            {
                button2.Enabled = false;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
            }
        }


        private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateBtnUi();

        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            Keyword keyword = listView1.SelectedItems[0].Tag as Keyword;
            KeywordForm keywordForm = new KeywordForm((int)keyword.Id);
            keywordForm.StartPosition = FormStartPosition.CenterScreen;
            if (keywordForm.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
                UpdateBtnUi();
            }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            Keyword keyword = listView1.SelectedItems[0].Tag as Keyword;
            using (Datastore datastore = new Datastore())
            {
                datastore.RemoveKeyword((int)keyword.Id);
                RefreshData();
                UpdateBtnUi();
            }
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ListView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            ListViewItem item = listView1.Items[e.Index];
            Keyword keyword = item.Tag as Keyword;
            if (e.NewValue == CheckState.Checked)
            {
                keyword.Enable = true;
            }
            else
            {
                keyword.Enable = false;
            }
            using (Datastore datastore = new Datastore())
            {
                datastore.UpdateKeyword(keyword);
            }
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            int selectIndex = listView1.SelectedItems[0].Index;
            Keyword keyword = keywords[selectIndex];
            Keyword keywordNext = keywords[selectIndex - 1];
            int sortby = keyword.SortBy;
            keyword.SortBy = keywordNext.SortBy;
            keywordNext.SortBy = sortby;
            using (Datastore datastore = new Datastore())
            {
                datastore.UpdateKeyword(keyword);
                datastore.UpdateKeyword(keywordNext);
            }
            RefreshData();
            UpdateBtnUi();
            listView1.Items[selectIndex].Selected = false;
            listView1.Items[selectIndex-1].Selected = true;
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            int selectIndex = listView1.SelectedItems[0].Index;
            Keyword keyword = keywords[selectIndex];
            Keyword keywordNext = keywords[selectIndex + 1];
            int sortby = keyword.SortBy;
            keyword.SortBy = keywordNext.SortBy;
            keywordNext.SortBy = sortby;
            using (Datastore datastore = new Datastore())
            {
                datastore.UpdateKeyword(keyword);
                datastore.UpdateKeyword(keywordNext);
            }
            RefreshData();
            UpdateBtnUi();
            listView1.Items[selectIndex].Selected = false;
            listView1.Items[selectIndex + 1].Selected = true;
        }
    }
}
